package com.zhou.bjgl.bjgl.thread;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhou.bjgl.bjgl.business_web.entity.BCourse;
import com.zhou.bjgl.bjgl.business_web.service.BCourseService;
import com.zhou.bjgl.bjgl.business_web.service.CommonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @ClassName: TimerThread
 * @Description:
 * @Author: zhou
 * @Date: 2022/2/8 16:11
 */
public class TimerThread implements Runnable{

    private static Logger LOG=LoggerFactory.getLogger(TimerThread.class);

    private Integer min;

    private BCourseService bCourseService;

    private String code;

    private CommonService commonService;

    private String activityName;

    private String appId;


    public TimerThread(String activityName,String appId,Integer min, BCourseService bCourseService, CommonService commonService,String code) {
        this.min = min;
        this.bCourseService = bCourseService;
        this.code=code;
        this.commonService=commonService;
        this.activityName=activityName;
        this.appId=appId;
    }

    @Override
    public void run() {
        //开始时间
        long start = System.currentTimeMillis();
        //结束时间
        final long end = start + min * 60 * 1000;

        final Timer timer = new Timer();
        //延迟0毫秒（即立即执行）开始，每隔1000毫秒执行一次
        timer.schedule(new TimerTask() {
            public void run() {
                LOG.info("MainActivity","此处实现倒计时，指定时长内, 5秒一次");
            }
        }, 0, 5000);
        //计时结束时候，停止全部timer计时计划任务
        timer.schedule(new TimerTask() {
            public void run() {
                QueryWrapper<BCourse> bCourseQueryWrapper = new QueryWrapper<>();
                bCourseQueryWrapper.eq("course_code",code);
                BCourse bCourse = new BCourse();
                bCourse.setStatus(1);
                bCourseService.update(bCourse,bCourseQueryWrapper);
                commonService.pushNoticeUtil(activityName,appId);
                timer.cancel();
            }

        }, new Date(end));
    }
}
